import Taro from "@tarojs/taro";
import http from "@/utils/http";
import { ref } from "vue";

export const globalState = {
  user: null,
  token: Taro.getStorageSync("token") || "",
  info: ref<any>({}),
  referralCode: "12",

  // 同步数据到 Storage
  setToken(str: string) {
    this.token = str;
    console.log("this", this);
    Taro.setStorageSync("token", str);
  },

  setUser(user: any) {
    this.user = user;
    Taro.setStorageSync("user", JSON.stringify(user));
  },

  getUserInfo() {
    http.post("/api/user/getUser").then((res:any) => {
      console.log("token获取用户", res);
      if(res.data[0].columns){
        this.info.value = res.data[0].columns;
        console.log("token获取用户", this.info.value);
        this.referralCode = this.info.value.referralCode
      }
    });
  }
};
